package org.qy.star_server.controller;

import lombok.extern.slf4j.Slf4j;
import org.qy.star_common.enums.ExchangeType;
import org.qy.star_server.core.BrokerServer;
import org.qy.star_server.core.Exchange;
import org.qy.star_server.core.VirtualHost;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/31 15:54
 **/
@RequestMapping("/exchange")
@RestController
@Slf4j
public class ExchangeController {

    public ExchangeController() {
    }

    @RequestMapping("/v-hostAll")
    public List<Exchange> getExchangeListWithinVirtualHost(String virtualHostName, String username) {
        List<Exchange> exchanges = null;
        if ("All".equals(virtualHostName)) {
            // 获取该用户的所有虚拟机
            List<VirtualHost> virtualHosts =  BrokerServer.memory.selectVirtualHostByUser(username);
            exchanges = new ArrayList<>();
            for (VirtualHost v : virtualHosts) {
                List<Exchange> exchangeList =  BrokerServer.memory.selectExchangeByVirtualHost(username,v.getVirtualHostName());
                exchanges.addAll(exchangeList);
            }
        } else {
            exchanges =  BrokerServer.memory.selectExchangeByVirtualHost(username,virtualHostName);
        }
        for (Exchange e : exchanges) {
            if (e.getExchangeType() == ExchangeType.DIRECT) {
                e.setExchangeTypeCN("direct");
            } else if (e.getExchangeType() == ExchangeType.TOPIC) {
                e.setExchangeTypeCN("topic");
            } else if (e.getExchangeType() == ExchangeType.FANOUT) {
                e.setExchangeTypeCN("fanout");
            }
        }
        return exchanges;
    }

    @RequestMapping("/add")
    public boolean addExchange(Exchange exchange) {


        if("".equals(exchange.getVirtualHostName()) || "/".equals(exchange.getVirtualHostName())) {
            return false;
        }
        if("DIRECT".equals(exchange.getExchangeTypeCN())) {
            exchange.setExchangeType(ExchangeType.DIRECT);
        } else if("TOPIC".equals(exchange.getExchangeTypeCN())) {
            exchange.setExchangeType(ExchangeType.TOPIC);
        } else if ("FANOUT".equals(exchange.getExchangeTypeCN())) {
            exchange.setExchangeType(ExchangeType.FANOUT);
        }
        System.out.println("exchange:" + exchange);
        try {
            // 1. 插入数据库
             BrokerServer.disk.insertExchange(exchange.getUsername(), exchange.getVirtualHostName(),exchange.getExchangeName(), exchange.getExchangeType(), exchange.isDurable(), exchange.isAutoDelete(), null);
            // 2. 插入内存
             BrokerServer.memory.insertExchange(exchange.getUsername(), exchange.getVirtualHostName(),exchange.getExchangeName(), exchange.getExchangeType(), exchange.isDurable(), exchange.isAutoDelete());
        } catch (Exception e) {
            log.error("交换机添加失败");
            return false;
        }
        return true;
    }
}
